package com.shuda.business.distribution.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shuda.business.distribution.entity.DistributionSchemeEntity;
import com.shuda.business.distribution.entity.DistributionRuleEntity;
import com.shuda.business.distribution.service.DistributionService;
import com.shuda.common.dto.DistributionSchemeDTO;
import com.shuda.common.dto.DistributionRuleDTO;
import com.shuda.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分发方案管理控制器
 */
@RestController
@RequestMapping("/api/distribution")
@RequiredArgsConstructor
@Tag(name = "分发方案管理", description = "分发方案管理相关接口")
public class DistributionController {

    private final DistributionService distributionService;

    /**
     * 获取分发方案列表
     */
    @GetMapping("/scheme/list")
    @Operation(summary = "获取分发方案列表")
    @PreAuthorize("hasAuthority('distribution:view')")
    public Result<IPage<DistributionSchemeEntity>> getSchemeList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "方案名称") @RequestParam(required = false) String schemeName,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        return Result.success(distributionService.getSchemeList(pageNum, pageSize, schemeName, status));
    }

    /**
     * 获取分发方案详情
     */
    @GetMapping("/scheme/{id}")
    @Operation(summary = "获取分发方案详情")
    @PreAuthorize("hasAuthority('distribution:view')")
    public Result<DistributionSchemeEntity> getSchemeById(@Parameter(description = "方案ID") @PathVariable Long id) {
        return Result.success(distributionService.getSchemeById(id));
    }

    /**
     * 创建分发方案
     */
    @PostMapping("/scheme")
    @Operation(summary = "创建分发方案")
    @PreAuthorize("hasAuthority('distribution:create')")
    public Result<Boolean> createScheme(@RequestBody DistributionSchemeDTO schemeDTO) {
        return Result.success(distributionService.createScheme(schemeDTO));
    }

    /**
     * 更新分发方案
     */
    @PutMapping("/scheme/{id}")
    @Operation(summary = "更新分发方案")
    @PreAuthorize("hasAuthority('distribution:update')")
    public Result<Boolean> updateScheme(
            @Parameter(description = "方案ID") @PathVariable Long id,
            @RequestBody DistributionSchemeDTO schemeDTO) {
        return Result.success(distributionService.updateScheme(id, schemeDTO));
    }

    /**
     * 删除分发方案
     */
    @DeleteMapping("/scheme/{id}")
    @Operation(summary = "删除分发方案")
    @PreAuthorize("hasAuthority('distribution:delete')")
    public Result<Boolean> deleteScheme(@Parameter(description = "方案ID") @PathVariable Long id) {
        return Result.success(distributionService.deleteScheme(id));
    }

    /**
     * 更新分发方案状态
     */
    @PutMapping("/scheme/{id}/status")
    @Operation(summary = "更新分发方案状态")
    @PreAuthorize("hasAuthority('distribution:update')")
    public Result<Boolean> updateSchemeStatus(
            @Parameter(description = "方案ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam Integer status) {
        return Result.success(distributionService.updateSchemeStatus(id, status));
    }

    /**
     * 获取分发规则列表
     */
    @GetMapping("/rule/list")
    @Operation(summary = "获取分发规则列表")
    @PreAuthorize("hasAuthority('distribution:view')")
    public Result<IPage<DistributionRuleEntity>> getRuleList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "方案ID") @RequestParam(required = false) Long schemeId,
            @Parameter(description = "规则名称") @RequestParam(required = false) String ruleName) {
        return Result.success(distributionService.getRuleList(pageNum, pageSize, schemeId, ruleName));
    }

    /**
     * 获取分发规则详情
     */
    @GetMapping("/rule/{id}")
    @Operation(summary = "获取分发规则详情")
    @PreAuthorize("hasAuthority('distribution:view')")
    public Result<DistributionRuleEntity> getRuleById(@Parameter(description = "规则ID") @PathVariable Long id) {
        return Result.success(distributionService.getRuleById(id));
    }

    /**
     * 创建分发规则
     */
    @PostMapping("/rule")
    @Operation(summary = "创建分发规则")
    @PreAuthorize("hasAuthority('distribution:create')")
    public Result<Boolean> createRule(@RequestBody DistributionRuleDTO ruleDTO) {
        return Result.success(distributionService.createRule(ruleDTO));
    }

    /**
     * 更新分发规则
     */
    @PutMapping("/rule/{id}")
    @Operation(summary = "更新分发规则")
    @PreAuthorize("hasAuthority('distribution:update')")
    public Result<Boolean> updateRule(
            @Parameter(description = "规则ID") @PathVariable Long id,
            @RequestBody DistributionRuleDTO ruleDTO) {
        return Result.success(distributionService.updateRule(id, ruleDTO));
    }

    /**
     * 删除分发规则
     */
    @DeleteMapping("/rule/{id}")
    @Operation(summary = "删除分发规则")
    @PreAuthorize("hasAuthority('distribution:delete')")
    public Result<Boolean> deleteRule(@Parameter(description = "规则ID") @PathVariable Long id) {
        return Result.success(distributionService.deleteRule(id));
    }

    /**
     * 获取分发规则选项列表
     */
    @GetMapping("/rule/options")
    @Operation(summary = "获取分发规则选项列表")
    @PreAuthorize("hasAuthority('distribution:view')")
    public Result<List<DistributionRuleEntity>> getRuleOptions(@Parameter(description = "方案ID") @RequestParam Long schemeId) {
        return Result.success(distributionService.getRuleOptions(schemeId));
    }
}